package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.common.utils.BeanHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author syf
 * @version 1.0
 * @date 2019/07/31 20:48
 * @Description TODO:品牌
 */
@Service
@Transactional
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 品牌列表信息
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    public PageResult<BrandDTO> pageQuery(String key, Integer page, Integer rows, String sortBy, Boolean desc) {

        try {
            //封装查询条件和排序条件对象
            Example example = new Example(Brand.class);
            //得到查询条件组装对象
            Example.Criteria criteria = example.createCriteria();
            //StringUtils.isNotBlank(key)判断key值部位null不为""
            if (StringUtils.isNotBlank(key)){
                //满足任意一条件都可搜索到内容
                criteria.orEqualTo("letter",key);
                criteria.orLike("id","%"+key+"%");
                criteria.orLike("name","%"+key+"%");
            }
            if (StringUtils.isNotBlank(sortBy)){
                //拼接排序
                example.setOrderByClause(sortBy+(desc ? " DESC":" ASC"));
            }
            //设置pageHelper参数
            PageHelper.startPage(page, rows);
            //进行mybatis查询
            List<Brand> brands = brandMapper.selectByExample(example);
            //获取pageHelper分页对象
            PageInfo<Brand> pageInfo = new PageInfo<>(brands);
            //创建返回值并转变成DTO
            return new PageResult<>(pageInfo.getTotal(), BeanHelper.copyWithCollection(pageInfo.getList(),BrandDTO.class));

        }catch (Exception e){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

    }

    /**
     * 新增页面
     * @param brandDTO
     * @param cids
     */

    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        try {
            //把dto转为pojo
            Brand brand=BeanHelper.copyProperties(brandDTO,Brand.class);
            brandMapper.insertSelective(brand);

            brandMapper.addBrandAndCategory(brand.getId(),cids);

        }catch (Exception e){
            throw  new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 商品列表：
     * 根据id查询品牌信息
     * @param id
     * @return
     */
    public BrandDTO findBrandById(Long id) {

        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand==null){
            //品牌不存在
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }

    /**
     * 根据分类id查询品牌信息
     * @return
     */
    public List<BrandDTO> findByCategory(Long id) {

        List<Brand> list=brandMapper.findByCategory(id);
        //判断是否为空
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,BrandDTO.class);
    }

    //根据品牌id批量查询品牌
    public List<BrandDTO> findByBrand(List<Long> ids) {
        List<Brand> brands = brandMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(brands)){
            //品牌不存在
            throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands,BrandDTO.class);
    }
}
